package com.jk.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jk.dao.DoctorDao;
import com.jk.pojo.DoctorBean;
import com.jk.utils.ReturnMsgUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author jisiqi
 * @version 1.0
 * @description: TODO
 * @date 2022/10/17 11:42
 */
@Service
public class DoctorService {

    @Autowired
    private DoctorDao doctorDao;

    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    public Map getDoctorList() {
        try {
            QueryWrapper<DoctorBean> Wrapper = new QueryWrapper<DoctorBean>();
            List<DoctorBean> list = doctorDao.selectList(Wrapper);
            System.out.println("==="+list);
            if (list.size()>0){
                list.forEach(DoctorBean ->{
                    elasticsearchRestTemplate.save(DoctorBean);
                });
            }
            return ReturnMsgUtils.returnMsg(200,"查看MySQL医生管理并新增es",list);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器错误",null);
        }
    }

    public Map queryEsList(DoctorBean doctorBean) {
        try {
            List<DoctorBean> list = new ArrayList<>();
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            //医生姓名模糊查
            if (doctorBean.getDoctorName()!=null && doctorBean.getDoctorName()!=""){
                boolQueryBuilder.must(QueryBuilders.matchQuery("doctorName",doctorBean.getDoctorName()));
            }
            //医生所属团队精确查
            if (doctorBean.getDoctorTeam()!=null && doctorBean.getDoctorTeam()!=-1){
                boolQueryBuilder.must(QueryBuilders.termQuery("doctorTeam",doctorBean.getDoctorTeam()));
            }
            //医生所属机构精确查
            if (doctorBean.getDoctorOrganization()!=null && doctorBean.getDoctorOrganization()!=-1){
                boolQueryBuilder.must(QueryBuilders.termQuery("doctorOrganization",doctorBean.getDoctorOrganization()));
            }
            NativeSearchQuery build = new NativeSearchQueryBuilder()
                    .withQuery(boolQueryBuilder)
                    .withHighlightFields(new HighlightBuilder.Field("doctorName").preTags("<font color='red'>").postTags("</font>"))
                    .build();
            SearchHits<DoctorBean> search = elasticsearchRestTemplate.search(build, DoctorBean.class);
            search.forEach(hit ->{
                DoctorBean content = hit.getContent();
                Map<String, List<String>> highlightFields = hit.getHighlightFields();
                hit.getContent().setDoctorName(highlightFields.get("doctorName")==null? hit.getContent().getDoctorName():highlightFields.get("doctorName").get(0));
                list.add(content);
            });
            return ReturnMsgUtils.returnMsg(200,"es中医生管理的数据",list);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器异常",null);
        }
    }

    public boolean deleteDoctorId(Integer id) {
        try {
//            doctorDao.deleteById(id);
            elasticsearchRestTemplate.delete(id.toString(), IndexCoordinates.of("doctor"));
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public boolean updateState(Integer id, Integer state) {
        try {
            DoctorBean doctorBean = new DoctorBean();
            doctorBean.setDoctorId(id);
            doctorBean.setDoctorState(state);
            doctorDao.updateById(doctorBean);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @Author: binzi
     * @Description:登录精确查
     * @Date: 2022/10/20 16:36
     * @Return: java.util.Map
     **/
    public Map login(DoctorBean bean) {
        DoctorBean user =  doctorDao.findDoctorByName(bean.getDoctorName());
        if (user == null){
            return ReturnMsgUtils.returnMsg(250,"账号错误",null);
        }
        //判断密码是否正确
        if(!user.getDoctor_pwd().equals(bean.getDoctor_pwd())){//密码错误
            return ReturnMsgUtils.returnMsg(251,"密码错误",null);
        }
        return ReturnMsgUtils.returnMsg(200,"登录成功",user);
    }
}
